require('es6-promise').polyfill();

import axios from 'axios'

function loadbillboard(data) {
  return {
    type: 'LOAD_BILLBOARD_DATA',
    data
  }
}


export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

export function createBillboardAsync(data) {
  return function(dispatch) {
    return axios.post('/api/billboard/create', data).then(
      resp => dispatch(loadBillboardAsync({
        limit: data.limit,
        offset: data.offset,
        type: data.type
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function deleteBillboardAsync(data) {
  return function(dispatch) {
    return axios.post('/api/billboard/delete', data).then(
      resp => dispatch(loadBillboardAsync({
        limit: data.limit,
        offset: data.offset,
        type: data.type
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}


export function loadBillboardAsync(data) {
  return function(dispatch) {
    return axios.post('/api/billboard', data).then(
      resp => dispatch(loadbillboard(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function updateStatusBillboardAsync(data) {
  return function(dispatch) {
    return axios.post('/api/billboard/updateStatus', data).then(
      resp => dispatch(loadBillboardAsync({
        limit: data.limit,
        offset: data.offset,
        type: data.type
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function updateBillboardAsync(data) {
  return function(dispatch) {
    return axios.post('/api/billboard/update', data).then(
      resp => dispatch(loadBillboardAsync({
        limit: data.limit,
        offset: data.offset,
        type: data.type
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}
